﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using ZFS.Library;
using ZFS.Library.Helper;
using ZFS.Model;
using ZFSData;
using ZFSData.InterFace.User;

namespace ZFS.ServerBusiness
{
    public class WCF_GroupManager : WCF_BaseManager<WCF_GroupManager>, IGroup
    {
        public tb_Group AddEntity(tb_Group entity)
        {
            var bytes = Server.AddEntityByGroup(ZipTools.CompressionObject(entity));
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public bool DeleteEntity(tb_Group entity)
        {
            byte[] bytes = Server.DeleteEntityByGroup(ZipTools.CompressionObject(entity));
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public bool ExistEntity(tb_Group entity)
        {
            var bytes = Server.ExistEntityByGroup(ZipTools.CompressionObject(entity));
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public List<tb_GroupFunc> GetGroupFuncs(string groupCode)
        {
            var bytes = Server.GetGroupFuncs(groupCode);
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public List<tb_Group> GetGroups(string search)
        {
            var bytes = Server.GetGroups(search);
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public List<View_GroupUser> GetGroupUsers(string groupID)
        {
            var bytes = Server.GetGroupUsers(groupID);
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public List<tb_Group> GetModels(tb_Group search)
        {
            var bytes = Server.GetModelsByGroup(ZipTools.CompressionObject(search));
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public List<tb_Group> GetPagingModels(int pageIndex, int pageSize, out int totalCount, tb_Group search, bool Asc = false)
        {
            var bytes = Server.GetPagingModelsByUser(pageIndex, pageSize, ZipTools.CompressionObject(search), Asc);
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
            {
                totalCount = response.TotalCount;
                return response.Results;
            }
            else
                throw new Exception(response.Message);
        }

        public bool Remove(int id)
        {
            var bytes = Server.RemovebyGroup(id);
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public bool UpdateEntity(tb_Group entity)
        {
            var bytes = Server.UpdateEntityByGroup(ZipTools.CompressionObject(entity));
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }

        public bool UpdateGroupFunc(tb_Group group, List<View_GroupUser> userList, List<tb_GroupFunc> funcList)
        {
            byte[] Group = ZipTools.CompressionObject(group);
            byte[] UserList = ZipTools.CompressionObject(userList);
            byte[] FuncList = ZipTools.CompressionObject(funcList);

            byte[] bytes = Server.UpdateGroupFunc(Group, UserList, FuncList);
            ServiceResponse response = ZipTools.DecompressionObject(bytes) as ServiceResponse;
            if (response.Success)
                return response.Results;
            else
                throw new Exception(response.Message);
        }
    }
}
